<!--
 * @Author: fangshengchu
 * @Date: 2024-05-14 15:22:27
 * @LastEditTime: 2024-06-12 10:45:25
 * @LastEditors: fangshengchu
 * @Description: 
 * @FilePath: \szhk-fe\src\views\purchaseArrival\arrivalRegistration\EditOrderDetail.vue
-->
<template>
  <contentModel v-if="modelValue" title="编辑" @close="cancel">
    <a-form
      ref="formRef"
      layout="vertical"
      :model="form"
      auto-label-width
      :rules="rules"
    >
      <a-row :gutter="16">
        <a-col :span="8">
          <a-form-item label="商务合同编号">
            <a-input :default-value="selectRow.orderNo" disabled />
          </a-form-item>
        </a-col>
        <a-col :span="8">
          <a-form-item field="billNo" label="订单号">
            <a-input :default-value="selectRow.sysOrderNo" disabled />
          </a-form-item>
        </a-col>
        <a-col :span="8">
          <a-form-item field="pmsArriveDate" label="订单类型">
            <a-input :default-value="selectRow.orderTypeName" disabled />
          </a-form-item>
        </a-col>
      </a-row>
      <a-row :gutter="16">
        <a-col :span="8">
          <a-form-item field="urgencyDegree" label="紧急程度">
            <dict-select
              v-model="form.urgencyDegree"
              allow-clear
              dict-type="urgencyDegree"
              placeholder="请选择"
              @change="changeUrgencyDegree"
            ></dict-select>
          </a-form-item>
        </a-col>
        <a-col :span="8">
          <a-form-item field="mdsVendorId" label="物料编码">
            <a-input :default-value="selectRow.mdsItemCode" disabled />
          </a-form-item>
        </a-col>
        <a-col :span="8">
          <a-form-item field="editUserCode" label="物料名称">
            <a-input :default-value="selectRow.mdsItemName" disabled />
          </a-form-item>
        </a-col>
      </a-row>
      <a-row :gutter="16">
        <a-col :span="8">
          <a-form-item field="editDate" label="订单数量（库存）">
            <a-input :default-value="selectRow.invOrdQty" disabled />
          </a-form-item>
        </a-col>
        <a-col :span="8">
          <a-form-item field="deliverType" label="可到货数量（库存）">
            <a-input :default-value="selectRow.invSureArriveQty" disabled />
          </a-form-item>
        </a-col>
        <a-col :span="8">
          <a-form-item
            field="mdsVendorId"
            label="基本计量单位"
            :validate-trigger="null"
          >
            <a-input :default-value="selectRow.invMdsUnitDesc" disabled />
          </a-form-item>
        </a-col>
      </a-row>
      <a-row :gutter="16">
        <a-col :span="8">
          <a-form-item field="pmsDeliverQty" label="本次到货数量（采购）">
            <a-input-number
              v-model="form.pmsDeliverQty"
              :min="0"
              @change="changeNumValue"
            ></a-input-number>
          </a-form-item>
        </a-col>
        <a-col :span="8">
          <a-form-item field="editDate" label="转换系数">
            <a-input :default-value="selectRow.mdsUnitRate" disabled />
          </a-form-item>
        </a-col>
        <a-col :span="8">
          <a-form-item field="editDate" label="订单数量（采购）">
            <a-input :default-value="selectRow.pmsOrdQty" disabled />
          </a-form-item>
        </a-col>
      </a-row>
      <a-row :gutter="16">
        <a-col :span="8">
          <a-form-item field="editDate" label="可到货数量（采购）">
            <a-input :default-value="selectRow.pmsSureArriveQty" disabled />
          </a-form-item>
        </a-col>
        <a-col :span="8">
          <a-form-item field="editDate" label="计量单位（采购）">
            <a-input :default-value="selectRow.pmsMdsUnitDesc" disabled />
          </a-form-item>
        </a-col>
        <a-col :span="8">
          <a-form-item field="invDeliverQty" label="本次到货数量（库存）">
            <a-input-number
              v-model="form.invDeliverQty"
              :min="0"
            ></a-input-number>
          </a-form-item>
        </a-col>
      </a-row>
      <a-row :gutter="16">
        <a-col :span="8">
          <a-form-item field="invoiceNo" label="发票号">
            <a-input v-model="form.invoiceNo" placeholder="请输入" />
          </a-form-item>
        </a-col>
        <a-col :span="8">
          <a-form-item field="proMdsItemId" label="机型">
            <inputModal
              v-model="form.proMdsItemId"
              :label-name="'materialCode'"
              :value-name="'id'"
              :label-value="form.proMdsItemCode"
              :title="'机型'"
              :type="'materialCode'"
              :default-query="{
                materialType: '6',
                materialStatus: '1,3',
              }"
              @handle-ok="changeProMdsItem"
            />
          </a-form-item>
        </a-col>
        <a-col :span="8">
          <a-form-item field="editDate" label="交付日期">
            <a-input :default-value="selectRow.planArrivalDateTime" disabled />
          </a-form-item>
        </a-col>
      </a-row>
      <a-row :gutter="16">
        <a-col :span="8">
          <a-form-item field="editDate" label="允许超交比例（%）">
            <a-input :default-value="selectRow.deliverOverproofRate" disabled />
          </a-form-item>
        </a-col>
        <a-col :span="8">
          <a-form-item field="editDate" label="项目号">
            <a-input :default-value="selectRow.mdsProjectCode" disabled />
          </a-form-item>
        </a-col>
        <a-col :span="8">
          <a-form-item field="editDate" label="客户类型">
            <a-input :default-value="selectRow.mdsCustomerTypeName" disabled />
          </a-form-item>
        </a-col>
      </a-row>
      <a-row :gutter="16">
        <a-col :span="8">
          <a-form-item field="editDate" label="发货时间">
            <a-input :default-value="selectRow.sendDate" disabled />
          </a-form-item>
        </a-col>
        <a-col :span="8">
          <a-form-item field="editDate" label="生产制造厂家">
            <a-input
              :default-value="selectRow.itemManufactureFactory"
              disabled
            />
          </a-form-item>
        </a-col>
        <a-col :span="8">
          <a-form-item field="editDate" label="生产制造厂家编码">
            <a-input
              :default-value="selectRow.itemManufactureFactoryCode"
              disabled
            />
          </a-form-item>
        </a-col>
        <a-col :span="8">
          <a-form-item field="editDate" label="经销商">
            <a-input :default-value="selectRow.dealerName" disabled />
          </a-form-item>
        </a-col>
      </a-row>
    </a-form>
    <template #footer>
      <a-space>
        <a-button type="outline" @click="handleCancel(record, rowIndex)"
          >取消</a-button
        >
        <a-button
          type="primary"
          :loading="loading"
          @click="handleSubmit(record, rowIndex)"
          >保存</a-button
        >
      </a-space>
    </template>
  </contentModel>
</template>

<script lang="ts" setup>
  import { defineAsyncComponent, reactive, ref } from 'vue';
  import dictSelect from '@/common/components/dict-select/index.vue';
  // import { Modal } from '@arco-design/web-vue';
  import inputModal from '@/common/components/search-modal/input-modal.vue';
  import lodash from 'lodash';

  const contentModel = defineAsyncComponent(
    () => import('@/common/components/content-model/index.vue')
  );
  const props = defineProps({
    modelValue: {
      type: Boolean,
      default: false,
    },
    // eslint-disable-next-line vue/require-prop-types
    selectId: {},
    selectRow: {
      type: Object,
      // eslint-disable-next-line @typescript-eslint/no-empty-function
      default: () => {},
    },
  });
  const emits = defineEmits(['update:modelValue', 'updateRow']);
  const cancel = () => {
    emits('update:modelValue', false);
  };
  // const { selectId } = toRefs(props);
  // const { cancel } = useModelFun();
  const formRef = ref(null);
  const loading = ref(false);
  let form = reactive({});
  const rules = {
    urgencyDegree: [
      {
        required: true,
        message: '紧急程度必填',
      },
    ],
    invDeliverQty: [
      {
        required: true,
        message: '本次到货数量（库存）必填',
      },
    ],
    pmsDeliverQty: [
      {
        required: true,
        message: '本次到货数量（采购）必填',
      },
    ],
  };
  const handleSubmit = async () => {
    const validate = await formRef.value.validate();
    if (validate) return;
    // 暂时去掉
    // if (
    //   Number(form.pmsDeliverQty) >
    //   props.selectRow.invSureArriveQty *
    //     (1 + props.selectRow.deliverOverproofRate) *
    //     props.selectRow.mdsUnitRate
    // ) {
    //   Modal.warning({
    //     title: '校验错误',
    //     content:
    //       '本次到货数量（库存）不可以超过可到货数量（采购）* 转换系数 * （1 + 允许超交比例（%））',
    //     okText: '确认',
    //   });
    //   return;
    // }
    emits('updateRow', lodash.cloneDeep(form));
    cancel();
  };
  const changeProMdsItem = (info) => {
    form.proMdsItemName = info.materialName;
    form.proMdsItemCode = info.materialCode;
  };
  const changeNumValue = (val) => {
    form.pmsDeliverQty = val;
    form.invDeliverQty = val;
  };
  const changeUrgencyDegree = (val) => {
    form.urgencyDegreeName = val.label;
  };
  // const handleOk = () => {
  //   cancel();
  // };
  const handleCancel = () => {
    cancel();
  };
  // eslint-disable-next-line @typescript-eslint/no-empty-function
  const init = () => {
    if (props.selectRow.urgencyDegree) {
      form = Object.assign(form, props.selectRow);
    } else {
      form.pmsDeliverQty = props.selectRow.invSureArriveQty;
      form.invDeliverQty = props.selectRow.invSureArriveQty;
      form.proMdsItemName = props.selectRow.proMdsItemName;
      form.proMdsItemCode = props.selectRow.proMdsItemCode;
      form.proMdsItemId = props.selectRow.proMdsItemId;
    }
  };
  init();
</script>
